package com.wyzz.hr.bo.impl;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IEmpTypeBO;
import com.wyzz.hr.dao.IEmpTypeDAO;
import com.wyzz.hr.pojo.Emptype;


public class EmpTypeBOImpl implements IEmpTypeBO {

	public EmpTypeBOImpl() {
		dao = null;
	}

	public List FindAllEmpType() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Emptype.class);
		dc.addOrder(Order.asc("emptypeSortId"));
		return dao.findByCriteria(dc);
	}

	public Emptype loadEmpTypById(String id, String fetch[]) {
		return (Emptype) dao.loadObject(com.wyzz.hr.pojo.Emptype.class, id,
				fetch, new boolean[0]);
	}

	public List FindEnabledEmpType() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Emptype.class);
		dc.add(Restrictions.eq(Emptype.PROP_EMPTYPE_STATUS, new Integer(1)));
		dc.addOrder(Order.asc("emptypeSortId"));
		return dao.findByCriteria(dc);
	}

	public boolean addEmptype(Emptype emp) {
		String trimmedEmptypeName = emp.getEmptypeName().trim();
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Emptype.class);
		dc.add(Restrictions.eq(Emptype.PROP_EMPTYPE_NAME, trimmedEmptypeName));
		List old = dao.findByCriteria(dc);
		if (old.isEmpty()) {
			dao.saveObject(emp);
			return true;
		}
		try {
			return false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean delEmptype(Class name, String id) {
		List check = new ArrayList();
		boolean isRefed = false;
		check = dao.exeHqlList((new StringBuilder()).append(
				"select count(*) from Empsalarypay where espEmptype = '")
				.append(id).append("'").toString());
		if (Integer.valueOf(check.get(0).toString()).intValue() > 0)
			isRefed = true;
		if (!isRefed) {
			check = dao.exeHqlList((new StringBuilder()).append(
					"select count(*) from Employee where empType = '").append(
					id).append("'").toString());
			if (Integer.valueOf(check.get(0).toString()).intValue() > 0)
				isRefed = true;
		}
		Emptype type = (Emptype) dao.loadObject(name, id, null, new boolean[0]);
		if (!isRefed && type != null) {
			dao.deleteObject(type);
			return true;
		}
		try {
			return false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public String updateEmptype(Emptype emptype) {
		if (emptype.getEmptypeStatus().intValue() == 0) {
			List check = new ArrayList();
			String hql = (new StringBuilder()).append(
					"select count(*) from Employee where empType = '").append(
					emptype.getId()).append("'").toString();
			check = dao.exeHqlList(hql);
			if (Integer.valueOf(check.get(0).toString()).intValue() > 0)
				return "1";
		}
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Emptype.class);
		dc.add(Restrictions.eq(Emptype.PROP_EMPTYPE_NAME, emptype
				.getEmptypeName().trim()));
		dc.add(Restrictions.ne(Emptype.PROP_ID, emptype.getId()));
		List old = dao.findByCriteria(dc);
		if (old.isEmpty()) {
			Emptype oldType = (Emptype) dao.loadObject(
					com.wyzz.hr.pojo.Emptype.class, emptype.getId(), null,
					new boolean[0]);
			oldType.setEmptypeName(emptype.getEmptypeName());
			oldType.setEmptypeDesc(emptype.getEmptypeDesc());
			oldType.setEmptypeStatus(emptype.getEmptypeStatus());
			oldType.setEmptypeSortId(emptype.getEmptypeSortId());
			dao.updateObject(oldType);
			return "0";
		} else {
			return "2";
		}
	}

	public void saveEmpTypeIdByBatch(String ids[]) {
		if (ids == null)
			return;
		int sort = 1;
		int len = ids.length;
		for (int i = 0; i < len; i++) {
			dao.exeHql((new StringBuilder()).append(
					"update Emptype set emptypeSortId=").append(sort).append(
					" where id ='").append(ids[i]).append("'").toString());
			sort++;
		}

	}

	public IEmpTypeDAO getDao() {
		return dao;
	}

	public void setDao(IEmpTypeDAO dao) {
		this.dao = dao;
	}

	private IEmpTypeDAO dao;
}
